package com.wandian.demo.other
{
	import flash.display.Bitmap;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import utils.Res;
	import utils.events.MenuEvent;

	public class MenuItem
	{
		private var _menuItemName:String;
		private var _menubtn:SimpleButton;
		private var _contentbitmap:Bitmap;
		private var _contentContainer:Sprite;
		private var menuContentX:Number=400;
		private var menuContentY:Number=0;
		public var boolShowContent:Boolean;
		public var mask:Sprite;

		public function MenuItem(menuName:String, downUrl:String, overUrl:String, contentUrl:String)
		{
			this._menuItemName=menuName;
			initMenuButton(downUrl, overUrl);
			this._contentbitmap=new Bitmap(Res.get(contentUrl));
//			this._contentbitmap.x=menuContentX;
//			this._contentbitmap.y=menuContentY;
			/*this.mask=new Sprite();
			this.mask.x=menuContentX;
			this.mask.y=menuContentY;			
			//			maskLayer.graphics.beginFill(0xff0f0f);
			mask.graphics.beginFill(0x660099, 0.2);
			//maskLayer.graphics.lineStyle(1, 0x660099);
			mask.graphics.drawRect(0, 0, 100,100);
			mask.graphics.endFill();
			//			maskLayer.height=390;
			//			maskLayer.width=789;
			mask.buttonMode=true;
			this._contentbitmap.mask=this.mask;*/
			_contentContainer=new Sprite();
			_contentContainer.addChild(_contentbitmap);
		}

		public function get contentContainer():Sprite
		{
			return _contentContainer;
		}

		public function set contentContainer(value:Sprite):void
		{
			_contentContainer = value;
		}

		public function get menubtn():SimpleButton
		{
			return _menubtn;
		}

		public function set menubtn(value:SimpleButton):void
		{
			_menubtn=value;
		}

		public function get contentbitmap():Bitmap
		{
			return _contentbitmap;
		}

		public function set contentbitmap(value:Bitmap):void
		{
			_contentbitmap=value;
		}

		public function initMenuButton(downUrl:String, overUrl:String):void
		{
			var btnDowMap:Bitmap=new Bitmap(Res.get(downUrl));
			var btnOverMap:Bitmap=new Bitmap(Res.get(overUrl));
			var btn:SimpleButton=new SimpleButton(btnDowMap, btnOverMap, btnOverMap, btnOverMap);
			btn.addEventListener(MouseEvent.CLICK, onBtnClickHandler);
			this._menubtn=btn;
			//_menubitmap.m
		}

		protected function onBtnClickHandler(event:MouseEvent):void
		{
			var menuArrays:Array=this._menubtn.parent["menuArrays"] as Array;
			var index:Number=menuArrays.indexOf(this);
			trace(index);
			// TODO Auto-generated method stub
			trace(" mouse click!" + menuItemName);
			var preMenu:MenuItem=MenuItem(this._menubtn.parent["curentContent"]);
			if (preMenu != null &&preMenu != this)
			{
				preMenu.boolShowContent=false;				
				if (this._menubtn.parent.contains(preMenu._contentContainer))
				{
					this._menubtn.parent.removeChild(preMenu._contentContainer);				
					var preindex:Number=menuArrays.indexOf(preMenu);
					trace("preindex:"+preindex);
					//下面的菜单y-		
					for (var i:int = preindex+1; i < menuArrays.length; i++) 
					{
						MenuItem(menuArrays[i]).menubtn.y-=preMenu._contentContainer.height; 
					}
				}

			}	
			
			//			this.mask.x=this._menubtn.x;
			//			this.mask.y=this._menubtn.y+this._menubtn.height;
			this._contentContainer.x=this._menubtn.x;
			this._contentContainer.y=this._menubtn.y+this._menubtn.height;
			
			if (boolShowContent)
			{
				this._menubtn.parent.removeChild(this._contentContainer);
				//下面的菜单y-		
				for (var k:int = index+1; k < menuArrays.length; k++) 
				{
					MenuItem(menuArrays[k]).menubtn.y-=this._contentContainer.height; 
				}
				
//				this.mask.width=0;
//				this.mask.height=0;
			}
			else
			{
				//下面的菜单y+
				this._menubtn.parent.addChild(this._contentContainer);
				for (var j:int = index+1; j < menuArrays.length; j++) 
				{
					MenuItem(menuArrays[j]).menubtn.y+=this._contentContainer.height; 
				}
			}
			boolShowContent=!boolShowContent;
			this._menubtn.parent["curentContent"]=this;
			this.menubtn.parent.dispatchEvent(new MenuEvent("onMenuItemClick", ""));
		}

		public function get menuItemName():String
		{
			return _menuItemName;
		}

		public function set menuItemName(value:String):void
		{
			_menuItemName=value;
		}

	}
}
